package com.wang.tool.lock.core;

import com.wang.tool.lock.LockConfigCenter;

import java.io.Serializable;

/**
 * @Description 锁请求体
 * @Author shaopeng
 * @Date 2022/6/10
 * @Version 1.0
 */
public class LockBody implements Serializable {
    private static final long serialVersionUID = -7447676386258846602L;

    public static final String COMMON_NAMESPACE = "common";

    /**
     * 命名空间
     */
    private String namespace;

    /**
     * 锁key
     */
    private String key;


    /**
     * 等锁时间 ms
     */
    private int lockWaitTime;


    /**
     * 锁超时时间
     * 默认30s
     */
    private int lockSecond;


    /**
     * 默认域加锁
     *
     * @param key
     * @return
     */
    public static LockBody build(String namespace, String key) {
        return build(namespace, key, LockConfigCenter.getDefLockWaitTime(), LockConfigCenter.getDefLockSecond());
    }


    /**
     * 加锁过程
     *
     * @param key
     * @param lockWaitTime
     * @return
     */
    public static LockBody build(String namespace, String key, int lockWaitTime, int lockSecond) {
        LockBody body = new LockBody();
        body.setNamespace(namespace);
        body.setKey(key);
        body.setLockWaitTime(lockWaitTime);
        body.setLockSecond(lockSecond);
        return body;
    }


    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public int getLockSecond() {
        return lockSecond;
    }

    public void setLockSecond(int lockSecond) {
        this.lockSecond = lockSecond;
    }

    public int getLockWaitTime() {
        return lockWaitTime;
    }

    public void setLockWaitTime(int lockWaitTime) {
        this.lockWaitTime = lockWaitTime;
    }

    public String getNamespace() {
        return namespace;
    }

    public void setNamespace(String namespace) {
        this.namespace = namespace;
    }

    @Override
    public String toString() {
        return "LockBody{" +
                ", key='" + key + '\'' +
                ", lockSecond=" + lockSecond +
                ", waitTime=" + lockWaitTime +
                ", namespace=" + namespace +
                '}';
    }
}
